﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abp.Domain.Repositories;
using KelonProud.Linq;
using KelonProud.TableManagement.Domain;

namespace KelonProud.TableManagement.Managers
{
    public class KelonDataBaseManager:KelonProudDomainService<KelonDataBase>
    {
        public KelonDataBaseManager(IRepository<KelonDataBase, int> manageRepository) : base(manageRepository)
        {
        }

        public int SaveDataBase(KelonDataBase dataBase)
        {
            CheckDataBaseExits(dataBase);

            var result = ManagerRepository.InsertOrUpdateAndGetId(dataBase);
            UnitOfWorkManager.Current.SaveChanges();

            return result;
        }

        private void CheckDataBaseExits(KelonDataBase dataBase)
        {
            if (List.Any(w => w.Id != dataBase.Id  && w.Name == dataBase.Name))
                throw new Exception("该名称数据源已存在".FormatWith(dataBase.Name));

        }
    }
}
